/*
 * @Description:
 * @Author: 胡涛
 * @Date: 2023-09-06 09:06:55
 * @LastEditors: 胡涛
 * @LastEditTime: 2024-07-19 00:27:08
 */
import { IButtonActionsItem, IFormPopupModel, ISerachFieldItem } from "@/types";
import { PaginationProps, TableColumnProps } from "ant-design-vue";
import { SortOrder } from "ant-design-vue/es/table/interface";

import { ref, reactive } from "vue";

type IListPageOptions<TModel> = {
  columns: Array<TableColumnOptions<TModel>>;
  searchFields: Array<ISerachFieldItem>;
  searchModel: any;
  buttonActions: Array<IButtonActionsItem>;
};

type TableColumnOptions<T> = TableColumnProps<T> & {
  children?: TableColumnOptions<T>[];
};
/**
 *
 */
export function useListPage<TModel, TKey>(
  options?: Partial<IListPageOptions<TModel>>
) {
  /**
   * 数据加载状态
   */
  let loading = ref(false);

  /**
   * 数据源
   */
  let dataSource = ref<Array<TModel>>([]);

  /**
   * 表格展示列
   */
  let columns = reactive<Array<TableColumnOptions<TModel>>>([]);

  /**
   * 分页数据
   */
  let pagination: PaginationProps = reactive({
    total: 0,
    current: 1,
    pageSize: 20,
    showTotal: (total, range) => {
      return `共 ${total} 条数据`;
    },
    showSizeChanger: true,
    pageSizeOptions: ["10", "15", "20", "30", "50", "100", "200", "300", "500"],
    onShowSizeChange: (current, pageSize) => (pagination.pageSize = pageSize),
  });

  /**
   * 选择行ID集合
   */
  let selectedRowKeys = ref<Array<TKey>>([]);

  /**
   * 当前选择行数据
   */
  let currentRow = ref<TModel>(null);

  /**
   * 排序方式
   */
  let sortedInfo: { columnKey: string; order: SortOrder } | null =
    reactive(null);

  /**
   * 筛选过滤信息
   */
  let filteredInfo: any = reactive<any>(null);

  /**
   * 查询参数对象
   */
  let searchModel = ref<Record<string, any>>({});

  /**
   * 验证规则
   */
  let searchFields = reactive<Array<ISerachFieldItem>>([]);

  /**
   * 按钮相关配置
   */
  let buttonActions = reactive<Array<IButtonActionsItem>>([]);

  /**
   * 获取跳过记录条数设置
   * @returns
   */
  let getSkipCount = () => {
    return getMaxResultCount() * ((pagination.current || 1) - 1);
  };

  /**
   * 获取最大返回记录条数
   * @returns
   */
  let getMaxResultCount = () => {
    return pagination.pageSize || 10;
  };

  /**
   * 获取当前选择的排序字段
   * @returns
   */
  let getSortingString = () => {
    if (sortedInfo) {
      return sortedInfo.columnKey + " " + sortedInfo.order;
    }
    return "";
  };

  /**
   * 判断当前是否有选择行
   * @returns
   */
  let hasSelected = () => {
    return selectedRowKeys.value.length > 0;
  };

  /**
   * 默认弹窗逻辑
   */
  let popupModel = reactive<IFormPopupModel<TModel, TKey>>({
    visible: false,
    action: "create",
  });

  /**
   * 默认添加数据方法事件处理
   */
  let onHandleCreate = () => {
    popupModel.action = "copy";
    popupModel.visible = true;
  };

  /**
   * 默认修改行方法事件处理
   * @param row
   */
  let onHandleUpdate = (row: any) => {
    popupModel.action = "edit";
    popupModel.data = row;
    popupModel.id = (row || {}).id;
    popupModel.visible = true;
  };

  /**
   * 默认预览行方法事件处理
   * @param row
   */
  let onHandlePreview = (row: any) => {
    popupModel.action = "preview";
    popupModel.data = row;
    popupModel.id = (row || {}).id;
    popupModel.visible = true;
  };

  /**
   * 默认复制行方法事件处理
   * @param row
   */
  let onHandleCopy = (row: any) => {
    popupModel.action = "copy";
    popupModel.data = row;
    popupModel.id = (row || {}).id;
    popupModel.visible = true;
  };

  let createQueryModel = (skipPaged?: boolean) => {
    if (skipPaged) {
      return { ...searchModel.value };
    }
    return {
      ...searchModel.value,
      maxResultCount: getMaxResultCount(),
      skipCount: getSkipCount(),
    };
  };

  if (options.columns) {
    columns = options.columns;
  }
  if (options.buttonActions) {
    buttonActions = options.buttonActions;
  }
  if (options.searchFields) {
    searchFields = options.searchFields;
  }
  if (options.searchModel) {
    searchModel.value = options.searchModel;
  }

  return {
    loading,
    dataSource,
    columns,
    pagination,
    selectedRowKeys,
    currentRow,
    sortedInfo,
    filteredInfo,
    searchModel,
    searchFields,
    buttonActions,
    popupModel,
    getMaxResultCount,
    getSkipCount,
    getSortingString,
    hasSelected,
    onHandleCreate,
    onHandleUpdate,
    onHandleCopy,
    onHandlePreview,
    createQueryModel,
  };
}
